package com.fand.configuration.service;

import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;

import com.fand.configuration.dao.VariableDao;
import com.fand.configuration.dto.VariableDto;
import com.google.common.collect.Maps;

/**
 * 变量服务
 * 
 * @author yaoHT
 */
public class DefaultVariableService implements VariableService {
	private ConfigurationService configurationService;
	
	private VariableDao variableDao;
	
	@Override
	@CacheEvict(value = VariableService.CACHE_NAME, allEntries = true)
	public void reload() {
	}
	
	@Override
	@Cacheable(value = VariableService.CACHE_NAME)
	public Map<String, String> saveVariableBy1ForMap() {
		final List<VariableDto> variableList = this.variableDao.selectVariableBy1ForMappedObjectList();
		
		final Map<String, String> variableMap = Maps.newHashMap();
		
		if (CollectionUtils.isEmpty(variableList)) { return variableMap; }
		
		for (final VariableDto variable : variableList) {
			variableMap.put(variable.getCode(), variable.getValue());
		}
		
		return variableMap;
	}
	
	@Override
	public String saveVariableBy1ForObject(final VariableDto variable) {
		return this.variableDao.saveVariableBy1ForObject(variable);
	}
	
	public void setConfigurationService(final ConfigurationService configurationService) {
		this.configurationService = configurationService;
	}
	
	public void setVariableDao(final VariableDao variableDao) {
		this.variableDao = variableDao;
	}
}